package cn.monkey.family.server.domain.shop.order;

import cn.monkey.family.data.Result;
import cn.monkey.family.data.context.Context;
import cn.monkey.family.data.context.RequestContext;
import cn.monkey.family.data.shop.order.OrderDto;
import cn.monkey.family.data.shop.order.OrderQueryRequest;
import cn.monkey.family.data.shop.order.OrderVo;
import com.google.common.base.Strings;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("shop/order")
public class OrderController {
    private final IOrderService orderService;

    public OrderController(IOrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping({"{id}", ""})
    Result<?> get(@Context RequestContext requestContext,
                  @PathVariable(value = "id", required = false) String id,
                  OrderQueryRequest orderQueryRequest,
                  @PageableDefault Pageable pageable) {
        if (!Strings.isNullOrEmpty(id)) {
            return this.orderService.readById(requestContext, id);
        }
        return this.orderService.readPage(requestContext, orderQueryRequest, pageable);
    }

    @PostMapping
    Result<OrderVo> create(@Context RequestContext requestContext,
                           @RequestBody OrderDto orderDto) {
        return this.orderService.create(requestContext, orderDto);
    }

    @PutMapping("{id}")
    Result<OrderVo> update(@Context RequestContext requestContext,
                           @PathVariable String id,
                           @RequestBody OrderDto orderDto) {
        return this.orderService.update(requestContext, id, orderDto);
    }

    @DeleteMapping("{id}")
    Result<Void> delete(@Context RequestContext requestContext,
                        @PathVariable String id) {
        return this.orderService.delete(requestContext, id);
    }
}
